package com.chuang.bootplus.controller.admin;

import com.alibaba.excel.EasyExcel;
import com.chuang.bootplus.base.utils.ApiResponse;
import com.chuang.bootplus.controller.admin.listener.document.DMapTReadListener;
import com.chuang.bootplus.controller.admin.listener.document.DMapWReadListener;
import com.chuang.bootplus.controller.admin.listener.document.DocumentReadListener;
import com.chuang.bootplus.controller.admin.listener.topic.TMapDReadListener;
import com.chuang.bootplus.controller.admin.listener.topic.TMapTReadListener;
import com.chuang.bootplus.controller.admin.listener.topic.TMapWReadListener;
import com.chuang.bootplus.controller.admin.listener.topic.TopicReadListener;
import com.chuang.bootplus.controller.admin.listener.word.WMapDReadListener;
import com.chuang.bootplus.controller.admin.listener.word.WMapTReadListener;
import com.chuang.bootplus.controller.admin.listener.word.WMapWReadListener;
import com.chuang.bootplus.controller.admin.listener.word.WordReadListener;
import com.chuang.bootplus.entity.document.DMapT;
import com.chuang.bootplus.entity.document.DMapW;
import com.chuang.bootplus.entity.document.Document;
import com.chuang.bootplus.entity.topic.TMapD;
import com.chuang.bootplus.entity.topic.TMapW;
import com.chuang.bootplus.entity.topic.Topic;
import com.chuang.bootplus.entity.word.WMapD;
import com.chuang.bootplus.entity.word.WMapT;
import com.chuang.bootplus.entity.word.WMapW;
import com.chuang.bootplus.entity.word.Word;
import com.chuang.bootplus.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;

import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;

@Api(tags = {"导入模块"})
@RestController
@RequestMapping("/import")
public class ImportController extends BaseController{

    //TODO 回滚问题，顺序问题
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/import")
    @ApiImplicitParam(value = "上传文件",dataType = "java.io.File",required = false)
    public ApiResponse addFile(@RequestPart(value = "WTDFile",required = true) MultipartFile[] wtdFile,
                               @RequestPart(value = "wordFile",required = false) MultipartFile[] wordFile,
                               @RequestPart(value = "documentFile",required = false) MultipartFile[] documentFile,
                               @RequestPart(value = "topicFile",required = false) MultipartFile[] topicFile,
                               @RequestParam(value = "fileTime",required = true) String fileTime)  {


        boolean b = addWTDFile(wtdFile,fileTime);
        if(!b) return new ApiResponse().setReCode("100").setReMsg("文件大小必须为3且顺序确定");

        addWordFile(wordFile);
        addDocumentFile(documentFile);
        addTopicFile(topicFile);
        return new ApiResponse();
    }


    @Transactional(rollbackFor = Exception.class)
    public boolean  addWTDFile(MultipartFile[] wtdFile,String time){



        // 0,w   1,ww  2,wt 3.wd
        boolean flag=false;
//        if (wtdFile.length < 3) return flag;
        try {
            System.out.println("addWTD");
            // 这里 需要指定写用哪个class去读，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
            // 如果这里想使用03 则 传入excelType参数即可
            if(wtdFile[0]!=null)EasyExcel.read(wtdFile[0].getInputStream(), Word.class, new WordReadListener(wordService,time)).sheet().doRead();
            if(wtdFile[1]!=null)EasyExcel.read(wtdFile[1].getInputStream(), Document.class, new DocumentReadListener(documentService,time)).sheet().doRead();
            if(wtdFile[2]!=null)EasyExcel.read(wtdFile[2].getInputStream(), Topic.class, new TopicReadListener(topicService,time)).sheet().doRead();
            flag=true;
        }catch (Exception e){
//            flag=false;
            e.printStackTrace();
        }
        return flag;
    }

    //TODO 1.DMapW  建议加上WName   2. DMapT  建议加上TName  0->DMapW   1->DMapT
    @Transactional(rollbackFor = Exception.class)
    public  void  addDocumentFile(MultipartFile[] documentFile){
        System.out.println("addD");
        if (documentFile==null) return;
        try {
            if(documentFile[0]!=null)EasyExcel.read(documentFile[0].getInputStream(), DMapW.class, new DMapWReadListener(dMapWService)).sheet().doRead();
            if(documentFile[1]!=null)EasyExcel.read(documentFile[1].getInputStream(), DMapT.class, new DMapTReadListener(dMapTService)).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();
        }
    }




    //TODO 1.DMapW   0->TMapW   1->TMapD  2->TMapTandTime
    @Transactional(rollbackFor = Exception.class)
    public  void  addTopicFile(MultipartFile[] topicFile){
        System.out.println("addT");
        if (topicFile==null) return;
        try {
            if(topicFile[0]!=null)EasyExcel.read(topicFile[0].getInputStream(), TMapW.class, new TMapWReadListener(tMapWService)).sheet().doRead();
            if(topicFile[1]!=null)EasyExcel.read(topicFile[1].getInputStream(), TMapD.class, new TMapDReadListener(tMapDService)).sheet().doRead();
            if(topicFile[2]!=null)EasyExcel.read(topicFile[2].getInputStream(), Topic.class, new TMapTReadListener(topicService)).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    //TODO 1.DMapW   0->WMapW   1->WMapD  2->WMapT 3->WMapTime
    @Transactional(rollbackFor = Exception.class)
    public  void  addWordFile(MultipartFile[] wordFile){
        System.out.println("addW");
        if (wordFile==null) return;
        try {
            if(wordFile[0]!=null)EasyExcel.read(wordFile[0].getInputStream(), WMapW.class, new WMapWReadListener(wMapWService)).sheet().doRead();
            if(wordFile[1]!=null)EasyExcel.read(wordFile[1].getInputStream(), WMapD.class, new WMapDReadListener(wMapDService)).sheet().doRead();
            if(wordFile[2]!=null)EasyExcel.read(wordFile[2].getInputStream(), WMapT.class, new WMapTReadListener(wMapTService)).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
